package org.yaac.server.egql;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Strings.isNullOrEmpty;
import static com.google.common.collect.Lists.newArrayList;

import java.util.List;

import org.antlr.runtime.ANTLRStringStream;
import org.antlr.runtime.CharStream;
import org.antlr.runtime.CommonTokenStream;
import org.antlr.runtime.RecognitionException;
import org.antlr.runtime.TokenStream;
import org.yaac.server.egql.antlr.EGQLLexer;
import org.yaac.server.egql.antlr.EGQLParser;

import com.google.common.base.Splitter;


/**
 * @author Max Zhu (thebbsky@gmail.com)
 *
 */
public class EGQLUtil {

	/**
	 * @param inputStr
	 * @return
	 * @throws RecognitionException 
	 */
	public static EGQLParser parser(String inputStr) {
		checkArgument(!isNullOrEmpty(inputStr));
		
		CharStream input = new ANTLRStringStream(inputStr);
		
		EGQLLexer lexer = new EGQLLexer(input);
		TokenStream tokens = new CommonTokenStream(lexer);
		return new EGQLParser(tokens);
	}
	
	/**
	 * @param input
	 * @return
	 */
	public static EntityProperty parseEntityProperty(String input) {
		checkArgument(!isNullOrEmpty(input));
		
		if (input.contains(".")) {
			List<String> inputs = newArrayList(Splitter.on('.').split(input));
			
			return new EntityProperty(inputs.get(0), inputs.get(1));
		} else {
			return new EntityProperty(null, input);
		}
	}
}
